package com.md.album.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.living.filedownloader.DownLoadManger
import com.living.player.db.RecentlyManger
import com.md.album.R
import com.md.album.adapter.SelectAudioAdapter
import com.md.album.bean.SelfWrapperAudio
import com.md.album.databinding.ActivitySelectAudioBinding
import com.md.album.db.SelfAlbumManager
import com.md.album.utils.AlbumUtil
import com.md.livingwidget.navigation.PHNavigationBar
import com.md.network.api.Audio

/**
 * @author liyue
 * created 2021/4/10
 * 为自建专辑选择音频
 * 注意！！！！
 * 为了简单，自建专辑添加的音频bean为Audio, 仅记录音频网络url，即使音频已经缓存在本地了也不记录其地址，故播放自建专辑的音频仅仅是在线播放。
 * todo 自建专辑支持添加本地音频，记录本地音频地址
 */
const val RECENT_PLAYER = 0x1
const val DOWNLOAD_AUDIO = 0x2
const val CREATED_ALBUM = 0x3
const val DATA_TYPE = "DATA_TYPE"
const val CURRENT_ALBUM_ID = "CURRENT_ALBUM_ID"
const val CURRENT_ALBUM_NAME="CURRENT_ALBUM_NAME"
const val SELF_ALBUM_AUDIO="SELF_ALBUM_AUDIO"

class SelectAudioActivity : AppCompatActivity() {
    private lateinit var binding: ActivitySelectAudioBinding
    private lateinit var recAudio: RecyclerView
    private lateinit var navBar: PHNavigationBar
    private lateinit var adapterAudio: SelectAudioAdapter
    private var audioList: List<SelfWrapperAudio> = ArrayList()
    private var albumId = "-1"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySelectAudioBinding.inflate(LayoutInflater.from(this))
        setContentView(binding.root)
        initView()
        initData()
    }

    private fun initView() {
        navBar = binding.navBar
        navBar.left1Imgback.apply {
            setImageResource(R.drawable.ph_common_back)
            setOnClickListener {
                finish()
            }
        }
        var tvTitle = navBar.centerTvTitle
        tvTitle.apply {
            text = getString(R.string.album_recent_play)
        }
        adapterAudio = SelectAudioAdapter(this, ArrayList())
        recAudio = binding.recAudio
        recAudio.apply {
            layoutManager = LinearLayoutManager(this@SelectAudioActivity)
            adapter = adapterAudio
        }

    }

    private fun initData() {
        var dataType = intent.getIntExtra(DATA_TYPE, -1)
        albumId = intent.getStringExtra(CURRENT_ALBUM_ID) ?: "-1"
        adapterAudio.setAlbumID(albumId.toInt())
        dataType?.let {
            audioList = getStatueAudios(dataType)
            adapterAudio.setData(audioList)
        }

        var tvTitle = navBar.centerTvTitle
        tvTitle.apply {
            if (dataType == RECENT_PLAYER) {
                text = getString(R.string.album_recent_play)
            } else if(dataType == DOWNLOAD_AUDIO){
                text = getString(R.string.album_download_audio)
            }else if(dataType == CREATED_ALBUM){
                if (intent.hasExtra(CURRENT_ALBUM_NAME)){
                    text = intent.getStringExtra(CURRENT_ALBUM_NAME)
                }
            }
        }

    }

    private fun getStatueAudios(type: Int): ArrayList<SelfWrapperAudio> {
        var result = ArrayList<SelfWrapperAudio>()
        var addedAudios = SelfAlbumManager.getIns().getAudiosByAlbumId(albumId)
        var addedMap = HashMap<Audio, String>()
        if (addedAudios.isNotEmpty()) {
            for (item in addedAudios) {
                addedMap[item] = item.id
            }
        }
        when (type) {
            RECENT_PLAYER -> {
                var recentAdios = RecentlyManger.getInstance().allRecentAudios
                transformData(recentAdios, addedMap, result)
            }

            DOWNLOAD_AUDIO -> {
                var downloadAudios = AlbumUtil.convertToAudioList(DownLoadManger.getInstance().downloadedList)
                transformData(downloadAudios, addedMap, result)
            }
            else -> {
               var selfAudios = intent.getParcelableArrayListExtra<Audio>(SELF_ALBUM_AUDIO)
                selfAudios?.let {
                    transformData(selfAudios, addedMap, result)
                }
            }


        }
        return result
    }

    private fun transformData(audios: List<Audio>, addedMap: HashMap<Audio, String>, result: ArrayList<SelfWrapperAudio>) {
        for (audio in audios) {
            var selfWrapperAudio = SelfWrapperAudio(audio, false)
            if (addedMap.containsValue(audio.id)) {
                selfWrapperAudio.setIsAdd(true)
            } else {
                selfWrapperAudio.setIsAdd(false)
            }
            result.add(selfWrapperAudio)
        }
    }

}